
package Administration;


import pojo.hibernate.Budgetheadmaster;
import pojo.hibernate.BudgetheadmasterDAO;
import pojo.hibernate.InstitutionmasterDAO;
import pojo.hibernate.Institutionmaster;
import utils.DevelopmentSupport;
import java.util.ArrayList;
import java.util.List;

public class ManageBudgetHeadAction extends DevelopmentSupport   {

    private Budgetheadmaster bhm = new Budgetheadmaster();
    private BudgetheadmasterDAO bhmDao = new BudgetheadmasterDAO();
    private List<Budgetheadmaster> bhmList = new ArrayList<Budgetheadmaster>();
    private InstitutionmasterDAO imDao = new InstitutionmasterDAO();
    private List<Institutionmaster> imList = new ArrayList<Institutionmaster>();
    private String message;
    private short BhmId;

    public void setbhm(Budgetheadmaster bhm) {
            this.bhm = bhm;
    }

    public Budgetheadmaster getbhm() {
            return bhm;
    }


    public void setBhmId(short BhmId) {
            this.BhmId = BhmId;
    }

    public short getBhmId() {
            return BhmId;
    }
    public List<Budgetheadmaster> getbhmList() {
        return bhmList;
    }
    public void setbhmList(List<Budgetheadmaster> bhmList) {
        this.bhmList = bhmList;
    }

   public List<Institutionmaster> getimList() {
        return this.imList;
    }

   public void setimList(List<Institutionmaster> imList) {
        this.imList = imList;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return this.message;
    }


 
  public void InitializeLOVs() {
        //Prepare LOV containing User Institutions
       imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
       //Prepare List containing Budget Head
       bhmList = bhmDao.findAll();
       return;
    }

    @Override

     public String execute() throws Exception {
        try {
           //Prepare LOV containing User Institutions
            imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
            return SUCCESS;
            } catch (Exception e) {
            message = "Exception in -> ManageBudgetHeadAxn"  + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

 public String Save() throws Exception {
        try {
            //If part saves record for the first time; else parts is for record update
            if (bhm.getBhmId()== null)
            {
                if(bhm.getInstitutionmaster().getImId()==null)
                 message = "Please select institution";
                 else if(bhm.getBhmName().length()==0)
                 message = "Please enter Budget Head Name";
                 else
                {
                bhmDao.save(bhm);
                message = "Budget Head created successfully for the institution.";
                }
                InitializeLOVs();
                bhm=null;
            }
            else
            {
              Budgetheadmaster bhm1 =bhmDao.findBybhmId(bhm.getBhmId());
              bhm1=bhm;
              bhmDao.update(bhm1);
              InitializeLOVs();
              message = "Budget Head updated successfully.";
            }
              return SUCCESS;
            }
       catch (Exception e)
          {
              InitializeLOVs();
              if (e.getCause().toString().contains("UNIQUE_BHM_IM_Id_BHM_Name"))
              message = "The budget head '" + bhm.getBhmName() + "' already exists for the selected institution";
              else
              message = "Exception in Save method -> ManageBudgetHeadAxn" + e.getMessage() + " Reported Cause is: " + e.getCause();
              return ERROR;
       }
}

 public String Fetch(){
        try {      
            //Prepare LOV containing Budget Head for the selected institution
            if(bhm.getInstitutionmaster().getImId()==null)
            message = "Please select institution";
            bhmList=bhmDao.findByImId(bhm.getInstitutionmaster().getImId());
            //Prepare LOV containing User Institutions
            imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
            return SUCCESS;
            }
        catch (Exception e)
            {
            message = "Exception in Delete method -> ManageBudgetHeadAxn" + e.getMessage()+ " Reported Cause is: " + e.getCause();
            return ERROR;
            }

    }

 public String Delete() throws Exception {
        try {
            //Retrieve the record to be deleted into bhm object
            bhm=bhmDao.findBybhmId(getBhmId());
            bhmDao.delete(bhm);
            //Prepare LOV containing User Institutions
            imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
             //Prepare LOV containing Budget Head for the selected institution
            bhmList=bhmDao.findByImId(bhm.getInstitutionmaster().getImId());
            message = "Record successfully deleted";
            bhm= null;
            return SUCCESS;
            } catch (Exception e) {
            message = "Exception in Delete method -> ManageBudgetHeadAxn" + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

  public String Edit() throws Exception {
              try {
                  //Retrieve the record to be updated into bhm object
                   bhm=bhmDao.findBybhmId(getBhmId());
                   //Prepare LOV containing User Institutions
                   imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
                   return SUCCESS;
                   } catch (Exception e) {
                   message = "Exception in Delete method -> ManageBudgetHeadAxn" + e.getMessage() + " Reported Cause is: " + e.getCause();
                   return ERROR;
                                          }
           }




public String Clear() throws Exception {
        try {
            bhm = null;
            imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));

            return SUCCESS;
        }   catch (Exception e) {
            message = "Exception in Clear -> ManageBudgetHeadAxn"  + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

}


